/*
 *  ChordPropertyCalclator.cpp
 *  SoundController0002
 *
 *  Created by jiro on 11/04/02.
 *  Copyright 2011 jiro music. All rights reserved.
 *
 */

#include "Calclator.h"

namespace sc { 

	double Calclator::CalcDistance( Note::Id note0, Note::Id note1 )
	{
		double distance = 0.0;		
		while ( true ) {
			if ( note0 == note1 ) {
				if ( distance > 6 ) {
					distance = 12 - distance;
				}
				break;
			}
			note0 = static_cast< Note::Id >( note0 + 7 );
			if ( note0 > 11 ) {
				note0 = static_cast< Note::Id>( note0 - 12 );
			}
			distance += 1;
		}
		return distance;
	}

	double Calclator::CalcComplex( const std::vector< Note::Id >& chord )
	{
		double complex = 0.0;
		size_t count = chord.size();
		for ( size_t i = 0; i < count; i++ ) {
			for ( size_t j = i + 1; j < count; j++ ) {
				complex += CalcDistance( chord[ i ], chord[ j ] );
			}
		}
		return complex;
	}
	
	double Calclator::CalcComplex( const std::set< Note::Id >& chord )
	{
		std::vector< Note::Id > chordVector;
		for ( std::set< Note::Id >::iterator iter = chord.begin(); iter != chord.end(); iter++ ) {
			chordVector.push_back( *iter );
		}
		return CalcComplex( chordVector );
		
	}

}

